package com.bjsdzk.common.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegExpUtils {

    public boolean match(String reg, String str) {
        return Pattern.matches(reg, str);
    }

    public static void main(String[] args) {
        String chineses = getChineses("The specified device group name already exists.");
        System.out.println(chineses);
    }
    public static String getChineses(String source) {
        String result = "";
        String pattern = "[\\u4E00-\\u9FA5\\u3002\\uff1b\\uff0c\\uff1a\\u201c\\u201d\\uff08\\uff09\\u3001\\uff1f\\u300a\\u300b]+";
        String[] splitStr = source.split("");
        for (String str : splitStr) {
            if (Pattern.matches(pattern, str))
                result += str;
        }
        return result;
    }

    public static List<Integer> findDate(String date) {
        String reg = "([0-9]+)";    // ( 为特殊字符，需要用 \\ 转义
        Pattern p = Pattern.compile(reg);
        Matcher m = p.matcher(date);
        List<Integer> result = new ArrayList<>();
        while (m.find()) {
            result.add(Integer.valueOf(m.group()));
        }

        return result;
    }

    public static List<String> find(String str) {
        String reg = "(?<=(?<!\\\\)\\$\\{)(.*?)(?=(?<!\\\\)\\})";
        return find(reg, str);
    }

    public static List<String> find(String reg, String str) {
        Matcher matcher = Pattern.compile(reg).matcher(str);
        List<String> list = new ArrayList<String>();
        while (matcher.find()) {
            list.add(matcher.group());
        }
        return list;
    }

    public static List<String> find(String reg, String str, int index) {
        Matcher matcher = Pattern.compile(reg).matcher(str);
        List<String> list = new ArrayList<String>();
        while (matcher.find()) {
            list.add(matcher.group(index));
        }
        return list;
    }

    public static String findString(String reg, String str, int index) {
        String returnStr = null;
        List<String> list = find(reg, str, index);
        if (list.size() != 0)
            returnStr = list.get(0);
        return returnStr;
    }

    public static String findString(String reg, String str) {
        String returnStr = null;
        List<String> list = find(reg, str);
        if (list.size() != 0)
            returnStr = list.get(0);
        return returnStr;
    }

    public static String getRealStr(String str, List<String> list) throws Exception {
        String reg = "\\{[\\^[1-9]\\d*$]*\\}";

        Pattern pattern = Pattern.compile(reg);
        Matcher matcher = pattern.matcher(str);

        while (matcher.find()) {
            String group = matcher.group();
            String indexStr = group.replace("{", "").replace("}", "");

            Integer index = Integer.valueOf(indexStr) - 1;
            String realStr = list.get(index);
            String replaceTarget = "{" + (index + 1) + "}";
            str = str.replace(replaceTarget, realStr);
        }

        return str;
    }
}